/**
 * File: FileIndexUtil.java
 * Created by: mhaimel
 * Created on: 14 Jul 2009
 * CVS:  $Id: FileIndexUtil.java,v 1.1 2009/08/05 15:16:11 mhaimel Exp $
 */
package uk.ac.ebi.curtain.util;

import java.io.Closeable;
import java.io.File;

import uk.ac.ebi.curtain.configuration.CurtainInputArguments;
import uk.ac.ebi.curtain.service.GlobalFileInfoIdx;
import uk.ac.ebi.curtain.util.FileIndexReader.IndexFilter;
import uk.ac.ebi.curtain.util.impl.FileIndexReaderImpl;
import uk.ac.ebi.curtain.util.impl.FileIndexWriterImpl;
import uk.ac.ebi.curtain.utils.data.FileType;
import uk.ac.ebi.curtain.utils.file.FileInfo;
import uk.ac.ebi.velvet.model.Strand;

/**
 * @author mhaimel
 *
 */
public class FileIndexUtil {
	
//	public static enum FileIndexControll{
//		SEP_CHAR('\t');
//		
//		private char id;
//		private String idStr;
//
//		private FileIndexControll(char id){
//			this.id = id;
//			this.idStr = Character.toString(id);
//		}
//		public String getIdAsString(){
//			return this.idStr;
//		}
//		public char getIdAsString(){
//			return this.idStr;
//		}
//		
//	}
	
	public static final char SEP_CHAR = '\t';
	public static final char CATEGORY_HEADER = 'T';
	public static final char FILE_HEADER = 'F';
	public static final char READ_HEADER = 'R';
	public static final char READ_POS_HEADER = 'P';
	public static final char CONTIG_HEADER = 'C';
	public static final char CONTIG_POS_HEADER = 'D';
	public static final char INSLEN_HEADER = 'I';
	public static final char INS_SD_HEADER = 'S';
	public static final char KMER_HEADER = 'K';
	
	public static final String SEP_CHAR_Str = Character.toString(SEP_CHAR);
	public static final String CATEGORY_HEADER_Str = Character.toString(CATEGORY_HEADER);
	public static final String FILE_HEADER_Str = Character.toString(FILE_HEADER);
	public static final String READ_HEADER_Str = Character.toString(READ_HEADER);
	public static final String READ_POS_HEADER_Str = Character.toString(READ_POS_HEADER);
	public static final String CONTIG_HEADER_Str = Character.toString(CONTIG_HEADER);
	public static final String CONTIG_POS_HEADER_Str = Character.toString(CONTIG_POS_HEADER);
	public static final String INSLEN_HEADER_Str = Character.toString(INSLEN_HEADER);
	public static final String INS_SD_HEADER_Str = Character.toString(INS_SD_HEADER);
	public static final String KMER_HEADER_Str = Character.toString(KMER_HEADER);

	
	public static FileIndexWriter buildWriter(File idxFile){
		FileIndexWriter writer = new FileIndexWriterImpl(idxFile);
		return writer;
	}
	
	public static FileIndexReader buildReader(File idxFile, FileIndexEvent handler){
		FileIndexReaderImpl reader = new FileIndexReaderImpl(idxFile);
		reader.setEventHandler(handler);
		return reader;
	}
	
	public static FileIndexReader buildReader(File idxFile, FileIndexEvent handler, IndexFilter filter){
		FileIndexReaderImpl reader = new FileIndexReaderImpl(idxFile);
		reader.setEventHandler(handler,filter);
		return reader;
	}
	
	public static void registerFiles(FileIndexReader out, CurtainInputArguments args, GlobalFileInfoIdx idx){
		for(FileInfo f : args.getFiles(FileType.values())){
			Integer id = idx.getId(f);
			out.registerFileId(id, f);
		}		
	}
	
	public static void registerFiles(FileIndexEvent out, CurtainInputArguments args, GlobalFileInfoIdx idx){
		for(FileInfo f : args.getFiles(FileType.values())){
			Integer cat = args.getCategory(f);
			Integer id = idx.getId(f);
			out.addFileId(id, f);
			out.addFile(f, cat);
			Integer insLen = args.getInsertLength(f);
			Integer insSd = args.getInsertLengthSd(f);
			if(null != insLen){
				out.addInsertLength(cat, insLen);
			}
			if(null != insSd){
				out.addInsertLengthSd(cat, insSd);
			}
		}		
	}
	
	
	public static interface FileIndexEvent extends Closeable{
		void addCategory(Integer category, String id);
		void addFileId(Integer fid, FileInfo fi);
		void addFile(FileInfo fi, Integer category);
		void addInsertLength(Integer category, Integer insLen);
		void addInsertLengthSd(Integer category, Integer sd);
		void addContig(FileInfo fi, String key, Long pos);
		void addContigPosition(FileInfo fi, Long pos);
		void addRead(FileInfo fi, String key, Strand strand, Long pos);
		void addReadPosition(FileInfo fi, Strand strand,Long pos);
	}
}
